package IC.SymbolTables;

import java.util.HashMap;
import java.util.Map;

public class SymbolTable {
	  protected Map<String,Symbol> entries;
	  public Map<String, Symbol> getEntries() {
		return entries;
	}

	public String getId() {
		return id;
	}

	public SymbolTable getParentSymbolTable() {
		return parentSymbolTable;
	}

	protected String id;
	  protected SymbolTable parentSymbolTable;
	  public SymbolTable(String id) {
	    this.id = id;
	    entries = new HashMap<String,Symbol>();
	  }
	  
	  public boolean insert(Symbol id)
	  {
		  if(entries.containsKey(id.getId())) return false;
		  entries.put(id.getId(),id);
		  return true;
	  }
	  
	  /* searches for id in the current symbol table, if not found
	   * searches in the parent symbol table, and if not found at parent
	   * the method returns null */
	  public Symbol lookup(String id)
	  {
		  //search in current scope
		  Symbol sid=entries.get(id);
		  if(sid!= null) return sid; //if found return
		  //search in parent scopes if exist, else return error
		  if(parentSymbolTable==null) return null;//didn't find symbol upto root
		  sid =parentSymbolTable.lookup(id); 
		  if(sid!=null) return sid; //if found in parent scope return
		  return null;
	  }

}
